package cn.le.crawler.jd;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class JdPropertyUtil {

    //地区
    private static final Map<String,Integer> AREA_MAP;
    static {
        AREA_MAP = new HashMap<String, Integer>() {{
            put("北京",1);put("重庆",4);put("河北",5);put("山西",6);put("辽宁",7);
            put("上海",2);put("吉林",8);put("黑龙江",9);put("内蒙古",10);put("江苏",11);
            put("天津",3);put("山东",12);put("安徽",13);put("浙江",14);put("福建",15);
            put("湖北",16);put("广东",17);put("广西",18);put("江西",19);put("四川",20);
            put("海南",21);put("贵州",22);put("云南",23);put("西藏",24);put("陕西",25);
            put("甘肃",26);put("青海",27);put("宁夏",28);put("新疆",29);
        }};
    }
    private static final String URL = "https://api.m.jd.com/api?appid=paimai-search-soa&functionId=paimai_unifiedSearch&body=";

    private static final String URL_SUFFIX = "&loginType=3&jsonp=jsonp_1583031449993_26998";

    static final String IMG_PREFIX  = "https://img14.360buyimg.com/imgb/s800x800_";

    private static final String IMG_URL = "https://api.m.jd.com/api?appid=paimai&functionId=getProductBasicInfo&body=";

    static final String ITEM_URL = "https://paimai.jd.com/";

    private static final String ADDRESS_URL = "https://api.m.jd.com/api?appid=paimai&functionId=getProductBasicInfo&body=";

    private static final String COMPETE_URL = "https://api.m.jd.com/api?appid=paimai&functionId=getPaimaiRealTimeData&body=";

    private static final int PAGE_SUM = 16;

    private static Map<String,Object> getBody(int pageNum,int provinceId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("apiType",2);
        paramMap.put("page",pageNum);
        paramMap.put("pageSize",40);
        paramMap.put("reqSource",0);
        paramMap.put("childrenCateId",12728);
        paramMap.put("provinceId",provinceId);
        return paramMap;
    }

    static List<String> getUrls(){
        List<String> urls = new LinkedList<>();
        AREA_MAP.forEach((key,val)->{
            for (int i = 1; i <= PAGE_SUM; i++) {
                  String url =  URL + JSON.toJSONString(getBody(i,val)) + URL_SUFFIX;
                  urls.add(url);
            }
        });
        return urls;
    }

    static String getImgUrl(long id){
        String param = "{\"paimaiId\":"+id+"}&loginType=3&jsonp=jsonp_1583031358966_94239";
        return IMG_URL + param;
    }


    /**
     * 竞争人数url
     * @param id
     * @return
     */
    static String getCompeteUrl(Long id){
        Map<String,Object> param = new HashMap<>();
        param.put("end",9);
        param.put("paimaiId",id);
        param.put("source",0);
        param.put("start",0);
        String paramStr = JSON.toJSONString(param);
        return COMPETE_URL + paramStr + "&loginType=3&jsonp=jsonp";
    }
}
